<?php
require_once("database.php");

$linkTableName = $_GET['linkTableName'];
$childTableName = $_GET['childTableName'];
$parentId = $_GET['id'];


$SQLlistQuery = "
SELECT childTable.* FROM best_newsImages AS childTable
LEFT JOIN $linkTableName AS linkTable ON linkTable.child_id = childTable.id
WHERE linkTable.parent_id IS NULL
   OR NOT EXISTS (SELECT child_id FROM $linkTableName WHERE parent_id = $parentId AND child_id = childTable.id)
GROUP BY childTable.id
ORDER BY childTable.id";

// In case the query above fails, you can uncomment below
// to pull all records from the childTable
//$SQLlistQuery = "SELECT * FROM best_newsImages";

$selectorString = "";
//echo $SQLlistQuery;
$SQLlistResult = $db->query($SQLlistQuery);
$selectorString = "";
while ($rowArray = $db->fetch_array($SQLlistResult)) {
		$sort_order = $rowArray["sort_order"];
		$title = $rowArray["title"];
		$image = $rowArray["image"];
		$image_thumb = $rowArray["image_thumb"];
		$id = $rowArray["id"];

		$selectorString .= "\n<div class='selectorRecord best_newsImages_record' id='$id'>";
		$selectorString .= "\n  ";
		$selectorString .= "\n  <div class='backListElement backListTitle'>$title</div>";
		$imgFileTypes = array('JPG','PEG','GIF','PNG');
		$fileEXT = strtoupper( substr($image, -3) );
		if ( !in_array($fileEXT, $imgFileTypes) ) {
			$selectorString .= "\n  <img src='images/defaultThumbnail.png' class='backListElement'>";
		} else if ($image_thumb) {
			$selectorString .= "\n  <img src='$image_thumb' class='backListElement'>";
		}
		
		$selectorString .= "</div>";
}

echo "<div class='innerSelector'>$selectorString</div>
<button type='button' class='saveSelection'>Save Selection</button>
<button type='button' class='cancelSelection'>Cancel</button>";
?>